package at.ac.lbg.media.vis.client.theme.render
{
	import at.ac.lbg.media.vis.client.theme.view.Artwork;
	import at.ac.lbg.media.vis.client.theme.view.Category;
	import at.ac.lbg.media.vis.client.theme.view.LegendCategory;
	
	import flare.display.DirtySprite;
	import flare.display.TextSprite;
	import flare.util.Colors;
	import flare.util.Shapes;
	import flare.vis.Visualization;
	import flare.vis.data.DataList;
	import flare.vis.data.DataSprite;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	
	import mx.collections.ArrayCollection;
	import mx.core.Application;
	import mx.core.UIComponent;

/**
 * Contains the Legend which is a Circle with all Subcategories.
 * 
 * */
	public class LegendRenderer extends UIComponent
	{
		private var selectedCats:ArrayCollection;
		
		private var mainCat:Category;
		private var wedgeDegrees:Number;
		private var wedgeRadian:Number;
		private var startRadian:Number;
		private var endRadian:Number;
		private var cat:Category;
		private var colorInactive:uint = Colors.rgba(220, 220, 220);
		private var colorInactiveHigh:uint  = Colors.darker(colorInactive, 1);
		private var colorActive:uint  = Colors.rgba(230, 130, 30);
		private var colorActiveHigh:uint  = Colors.rgba(255, 255, 255);
		private var label:TextSprite;
		private var vis:Visualization;
		private var legend:UIComponent;
		
		public function LegendRenderer(cs:ArrayCollection)
		{
			super();
			selectedCats = cs;
			
			vis = Application.application.tviz.visualization;
			legend = new UIComponent();
			//legend.width = 280;
            //draw(legend);
            addChild(legend);
            addEventListener(Event.RENDER, drawEvent);
		}
		
		public function redraw(highlightCatIds:ArrayCollection = null):void {
			// make all vis nodes (artworks) dirty, the easiest way to get them rendered again
			if (Application.application.tviz.visualization.data) {
				var nodes:DataList = Application.application.tviz.visualization.data.nodes;
				if (nodes) {
					for (var r:int = 0; r < nodes.length; r++) {
						var n:DataSprite = nodes[r];
						n.dirty();
					}
				}
			}
			
			// remove legend sprite to get a clear canvas
			this.removeChildAt(0);
			legend = null;
			legend = new UIComponent();
			//legend.width = 280;
			addChild(legend);
			draw(highlightCatIds);
			DirtySprite.renderDirty();
		}
		
		public function drawEvent(event:Event):void {
			draw();
			// this eventlistener is removed after the initial screen draw.
			removeEventListener(Event.RENDER, drawEvent);
		}

		/**
		 * displays only the selected Categories from the Tree Menu
		 **/
        private function draw(highlightCatIds:ArrayCollection = null):void {
        	if (!selectedCats) {
        		return;
        	}

			catsNum = selectedCats.length;

			var catsNum:uint;
			var diskSize:int = 13;
			var startDisk:int = 0;
			var endDisk:int = 13;
			var g:Graphics;
			var high:Boolean = false;

			
			wedgeDegrees = 360 / catsNum;
			wedgeRadian = Math.PI / 180 * wedgeDegrees;	
			
			legend.graphics.clear();

			for (var k:int = 0; k < catsNum; k++) {
				//var hbox:HBox = new HBox;
				//legend.addChild(hbox);
				
				startRadian = 0;
				endRadian = wedgeRadian;
				
					var catSprite:LegendCategory = new LegendCategory();
					//catSprite.width = 280;
					catSprite.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
					legend.addChild(catSprite);
					var label:TextField = new TextField();

					label.text = selectedCats[k].name;
					label.y = k*33 - 10;
					label.x = 30;
					legend.addChild(label);
					catSprite.title = selectedCats[k].name;
					catSprite.catId = selectedCats[k].id;
					
					if (highlightCatIds) {
						if (highlightCatIds.contains(catSprite.catId)) {
							//catSprite.filters = [new GlowFilter(Colors.rgba(230, 130, 30), 0.7, 6, 6, 3, 1, false, false)];
							high = true;
						} else {
							high = false;
						}
					}
					
					g = catSprite.graphics;
					g.clear();
					
					// draw one wedge per category
					for (var i:int = 0; i < catsNum; i++) {
						if (k == i) {
							if (high) {
								g.beginFill(colorActiveHigh, 1);
							} else {
								g.beginFill(colorActive, 1);
							}
						} else {
							if (high) {
								g.beginFill(colorInactiveHigh, 1);
							} else {
								g.beginFill(colorInactive, 1);
							}
						}
						Shapes.drawWedge(g, 10, k*33, endDisk, startDisk, startRadian, endRadian);
						startRadian += wedgeRadian;
						endRadian += wedgeRadian;
					}
					//g.drawCircle(10, k*33, diskSize);
					g.endFill();
					
			}
			
        }
        public function onMouseOver(event:MouseEvent):void {
			var lg:LegendCategory = LegendCategory(event.target);
            lg.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
            lg.filters = [new GlowFilter(Colors.rgba(230, 130, 30), 0.7, 6, 6, 3, 1, false, false)];
			vis.data.visit(function(d:DataSprite):void {
				var aw:Artwork = d as Artwork;
				if (aw.categoryIds.contains(lg.catId)) {
					aw.highlightMe = lg.catId;
				}
			});
			ArtworkRenderer.instance.stacks.removeAll();
			DirtySprite.renderDirty();
		}
		
		private function onMouseOut(event:MouseEvent):void {
			var sprite:Sprite = Sprite(event.target);
			sprite.filters = null;
			vis.data.visit(function(d:DataSprite):void {
				var aw:Artwork = d as Artwork;
				aw.highlightMe = -1;
			});
			ArtworkRenderer.instance.stacks.removeAll();
			DirtySprite.renderDirty();
		}

	}
}